<?php
namespace app\service;
use think\facade\Db;
use app\model\BankModel;
use Exception;

class BankService{  
    public function list($params){
        $page = $params['page'];
        $pageCount = $params['page_count'];
        $data = BankModel::page($page,$pageCount)->order('id','desc')->select(); 
        $count = BankModel::count(); 
        return ["list"=>$data,"count"=>$count];
    } 

    public function all(){
        $banks = BankModel::select(); 
        return $banks;
    }
    
    public function info($id){
        $banks = BankModel::where('id',$id)->select(); 
        return $banks;
    }

    public function save($params){              
        if(empty($params['id'])){
            $data = BankModel::where('bank_name',$params['bank_name'])->find();
            if($data){
                throw new Exception("帐户名称已存在");
            }
            $data = BankModel::create($params); 
        }else{
            $data = BankModel::update($params, ['id' => $params['id']]); 
        }        
        return $data;
    }

    public function delete($params){   
        $data = BankModel::destroy($params['id']);         
        return $data;
    }
}